<?php
	session_start();
	require_once('../include/connect.inc.php');
if($_SESSION['sess_id'] <> session_id()){
?>
 <SCRIPT LANGUAGE="JavaScript">
			alert("กรุณาเข้าสู่ระบบให้ถูกต้องตามขั้นตอน...!");
			location="login.php";
</script>
<?php }?>
<?php
$n=0;
$s= 0;
$p=0;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Model Test Program</title>
<link rel="stylesheet" type="text/css" href="../template/template_style_wood.css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<body>
<div id="menu"><div><img src="../images/icon-m.png" /></div>
<div id="menulinks">
		<a  href="../person_tea.php">ข้อมูลส่วนตัว</a>
		<a href="../tea_edit.php">เปลี่ยนรหัสผ่านใหม่</a>
        
        <a href="show_subject.php">จัดการรายวิชา</a>
		<a  href="../teacher_test.php">จัดการข้อสอบ</a>
        <a  href="../teacher_tests.php">จัดการชุดข้อสอบ</a>
        <a  href="../agree_set.php">กำหนดสิทธิ์</a>
		<a class="active" href="../exam/tea_chack.php">ตรวจสอบคะแนน</a>
        <a href="../news/manage_news.php">ประกาศข่าวสาร</a>
        <a href="../problem/send_problem.php">แสดงความคิดเห็น</a>
        <a href="../logout.php">ออกจากระบบ</a>
		</div></div>           
 <div id="p3">    
<div id="header"></div>
   <div id="page"><div id="p1">คุณ<?php echo $_SESSION['teacher_name'];?><hr/>
<form name="form1" method="post" action="">
<table class="p21" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <th align="left">ตรวจสอบคะแนน</th>
        <th align="left"><a href="tea_chack.php"><img src="../images/undo.png" border="0" /></a></th>
      </tr>
    </table>
 <table width="100%" border="0"  id="table" >
    <tr>
      <th width="78" align="center">ลำดับ</th>
      <th width="99" align="center">รหัสผู้ทำข้อสอบ</th>
       <th width="121" align="center" valign="top">ชื่อ-สกุล</th>
       <th width="155" align="center">คะแนนที่ทำได้/คะแนนเต็ม</th>
       <th width="105" align="center">คิดเป็นเปอร์เซ็นต์</th>
       <th width="106" align="center">เมื่อวันที่</th>
    </tr>
<?php
$max = query("select max(sumscore) from tb_score where set_id='".$_GET['set_id']."' ");
	$total_max = mysql_result($max,0);
	echo "<font color=\"FF0000\" size=\"+2\"><strong>คะแนนสูงสุดของชุด : </strong></font>";
	echo $total_max=number_format($total_max,2,'.',' ');
	echo "<br>";
$min = query("select min(sumscore) from tb_score where set_id='".$_GET['set_id']."' ");
	$total_min = mysql_result($min,0);
	echo "<font color=\"000000\" size=\"+2\"><strong>คะแนนต่ำสุดของชุด : </strong></font>";
	echo $total_min=number_format($total_min,2,'.',' ');
	echo "<br>";
?>
<?php
$filName = "report.csv";
$objWrite = fopen("report.csv", "w");
//fwrite($objWrite, "\"รหัสผู้ทำข้อสอบ\",\"ชื่อ-นามสกุล\",\"คะแนนที่ได้\",\"คิดเป็นเปอร์เซนต์\",\"วันที่ทำข้อสอบ\" \n");		
$number=0; 
$query= query(" SELECT
tb_score.score_id,
tb_score.r_student_id,
tb_score.sumscore,
tb_score.set_id,
tb_score.dotest_id,
tb_score.dates,
tb_score.max_score,
tb_student.student_fname,
tb_student.student_lname,
tb_set_of_test.rangetest_id,
tb_set_of_test.subject_id,
tb_set_of_test.topic_id,
tb_set_of_test.set_sum_question
FROM
tb_score
Inner Join tb_student ON tb_score.r_student_id = tb_student.r_student_id
Inner Join tb_set_of_test ON tb_score.set_id = tb_set_of_test.set_id and tb_score.set_id=' ".$_GET[set_id]." ' ORDER BY r_student_id asc");
	while ($rs= fetch_array($query)){
		$r_student_id= $rs['r_student_id'];
		$student_fname=$rs['student_fname']."  ".$rs['student_lname'];
		$set_sum_question=$rs['set_sum_question'];
		$score=$rs['sumscore'];
		$set_id = $rs['set_id'];	
		$dates = $rs['dates'];	
		$max_score=$rs['max_score'];
		$number++;
		$p++;
		$s++;
		$sumscore=$sumscore+$score;
?>
    <tr>
       <td align="center"><? echo "$number"; echo ".";?></td>
       <td align="center"><? echo "$r_student_id";?></td>
        <td align="center"><? echo "$student_fname ";?></td>
        <td align="center"><? echo "$score";?>/<? echo "$max_score"?></td>
        <td align="center">
        <?php
        $per=($score/$max_score)*100;
		echo $per=number_format($per,2,'.',' ');
		echo "%";
		?> </td>
        <td align="center"><? echo "$dates";?></td>
    </tr>
<?php
fwrite($objWrite, "\"$r_student_id\",\"$student_fname\",\"$score\",\"$per\",\"$dates\" \n");
?>
<?php }?>
      <tr>
      		<th></th>
            <th></th>
            <th></th>
            <th></th>
             <th></th>
             <th></th>
       </tr>
       <tr>
      		<th>ข้อมูลสถิติ :</th>
            <td></td>
            <td align="right">&nbsp;</td>
            <td align="center"><font size="+1" color="#0000FF"><strong>ค่าเฉลี่ย=</strong></font>
			<?php 
			//หาค่าเฉลี่ยคะแนน
			$query=query("SELECT AVG(sumscore) as average FROM tb_score where set_id=$set_id");
				while ($rs= fetch_array($query)){
					$avg=$rs['average'];
					echo $avg=number_format($avg,2,'.',' ');
					}
			?>
            </td>
             <td></td>
             <td align="center">
             <?php
			 //fwrite($objWrite, "\"คะแนนเต็ม\",\"$max_score\" \n");
			fclose($objWrite);
			echo "<br>ดาวน์โหลดผลการสอบ<br><a href=$filName>Download</a>";
			?>
            </td>
       </tr>
  </table>
</form>
    </div>
    </div>
<div id="footer"></div>
</div>
</body>
</html>
